(function($){

    $.fn.extend({
        multiplier : function(options, arg) {
            if (typeof(options) == "object") {
                options = $.extend({}, $.multiplier.defaults, options);
            }
            this.each(function() {
                for (var fld in options) {
                    jQuery(this).attr("plugin_multiplier_" + fld, options[fld]);
                }
                new $.multiplier(this, options, arg);
            });
            return;
        }
    });

    $.multiplier = function(item, options, arg) {
        var myName = item.name;
        var multiSpan = jQuery("span[ref='" + myName + "']");
        var base = parseInt(jQuery(item).attr("base"), 10);
        var val = parseInt(jQuery(item).val(), 10);
        var result = base * val;
        if (options.aftercalc != null) {
            result = options.aftercalc(result, base, val);
        }
        multiSpan.html(formatNumbers(result));
        if (typeof(options) == "string") {
            var methodName = options;
            options = {};
            for (var fld in $.multiplier.defaults) {
                options[fld] = jQuery(item).attr("plugin_multiplier_" + fld);
            }
            // methods
        } else {
            jQuery(item).change(function() {
                if (multiSpan.size() == 0) { return; }
                var base = parseInt(jQuery(item).attr("base"), 10);
                var val = parseInt(jQuery(item).val(), 10);
                var result = base * val;
                if (options.aftercalc != null) {
                    result = options.aftercalc(result, base, val);
                }
                multiSpan.html(formatNumbers(result));
            });
        }

        $.multiplier.defaults = {
            aftercalc : null
        }
    }
})(jQuery);
